import Vue from "vue";
import VueRouter from "vue-router";
import store from "../store/index.js";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    name: "index",
    component: () => import("../views/student/index.vue"),
    children: [
      {
        path: "/student",
        name: "student",
        component: () => import("../views/student/student.vue"),
      },
      {
        path: "/teacher",
        name: "teacher",
        component: () => import("../views/student/teacher.vue"),
      },
      {
        path: "/helper",
        name: "helper",
        component: () => import("../views/student/helper.vue"),
      },
    ],
  },
  {
    path: "/index",
    name: "index1",
    component: () => import("../views/teach/index.vue"),
  },
  {
    path: "/login",
    name: "login",
    component: () => import("../views/login.vue"),
  },
];

const router = new VueRouter({
  routes,
});
router.beforeEach((to, from, next) => {
  if (store.state.token) {
    next();
  } else {
    if (to.path == "/login") {
      next();
    } else {
      next("/login");
    }
  }
});

const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};
export default router;
